using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace PiratesOfBermuda
{
    public abstract class MySprite : DrawableGameComponent
    {
        #region Fields & Accessors
        protected string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        protected MyPiratesGame currentGame;
        protected SpriteBatch spriteBatch;

        public Texture2D Texture;

        protected List<Rectangle> frames = new List<Rectangle>();

        //Kich thuoc frame cua sprite
        protected int frameWidth = 0;
        protected int frameHeight = 0;
        //Frame hien tai
        protected int currentFrame = 0;
        //Thoi gian 1 frame ton tai tren man hinh
        public float frameTime = 0.08f;
        protected float timeForCurrentFrame = 0.0f;
        //So luong frame ma Animation cua sprite co
        public Point Size;
        //Vi tri cua sprite tren ban do
        public int X { get; set; }
        public int Y { get; set; }

        //Tinh trang cua sprite
        public bool Destroyed = false;

        public Color tintColor = Color.White;

        //Property Accessor
        public float FrameTime
        {
            get { return frameTime; }
            set { frameTime = MathHelper.Max(0, value); }
        }

        public virtual Rectangle Source
        {
            get
            {
                if (frames.Count == 0)
                    return Rectangle.Empty;
                return frames[currentFrame];
            }
        }

        public virtual Rectangle Destination
        {
            get
            {
                return new Rectangle(
                Constants.InitialPositionX + Constants.MapPieceWidth * X,
                Constants.InitialPositionY + Constants.MapPieceHeight * Y,
                Constants.MapPieceWidth, Constants.MapPieceHeight);
            }
        }

        #endregion

        #region Initialize
        //Constructor
        public MySprite(Game game, SpriteBatch spriteBatch)
            : base(game)
        {
            currentGame = (MyPiratesGame)game;
            this.spriteBatch = spriteBatch;
        }


        protected void Initialize(string textureName, Rectangle initialFrame, Point size, Point position)
        {
            this.Size = size;
            Texture = currentGame.Content.Load<Texture2D>(textureName);
            frameWidth = initialFrame.Width;
            frameHeight = initialFrame.Height;
            X = position.X;
            Y = position.Y;

            InitializeFrames(initialFrame, size);
        }

        //Khoi tao cac frame
        private void InitializeFrames(Rectangle initialFrame, Point size)
        {
            if (size.X == 1)
            {
                for (int i = 0; i < size.Y; i++)
                {
                    frames.Add(
                        new Rectangle(
                            initialFrame.X,
                            initialFrame.Y + (initialFrame.Height * i),
                            initialFrame.Width,
                            initialFrame.Height));

                }
            }
            else if (size.Y == 1)
            {
                for (int i = 0; i < size.X; i++)
                {
                    frames.Add(
                        new Rectangle(
                            initialFrame.X + (initialFrame.Width * i),
                            initialFrame.Y,
                            initialFrame.Width,
                            initialFrame.Height));

                }
            }
                
        }
        #endregion 

        #region Update and Render
        public override void Update(GameTime gameTime)
        {

            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            timeForCurrentFrame += elapsed;

            if (timeForCurrentFrame >= FrameTime)
            {
                currentFrame = (currentFrame + 1) % (frames.Count);
                timeForCurrentFrame = 0.0f;
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if(Source != Rectangle.Empty)
                spriteBatch.Draw(Texture, Destination, Source, tintColor);
            base.Draw(gameTime);
        }
        #endregion
    }
}
